package cn.cqautotest.cqviehelper.ui.fragment;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;

import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.adapters.ImageBannerAdapter;
import cn.cqautotest.cqviehelper.adapters.MenuItemAdapter;
import cn.cqautotest.cqviehelper.base.BaseActivity;
import cn.cqautotest.cqviehelper.base.BaseFragment;
import cn.cqautotest.cqviehelper.beans.SlideShow;
import cn.cqautotest.cqviehelper.ui.activity.HomeActivity;

import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import org.yczbj.ycrefreshviewlib.YCRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.view.View.OVER_SCROLL_NEVER;

@SuppressLint("NonConstantResourceId")
public class MenuFragment extends BaseFragment<BaseActivity> {

    @BindView(R.id.container)
    View container;
    @BindView(R.id.banner)
    Banner<SlideShow.DataBean, ImageBannerAdapter> mBanner;
    @BindView(R.id.recyclerView)
    YCRefreshView recyclerView;
    private MenuItemAdapter mAdapter;
    private List<SlideShow.DataBean> mDataBeans = new ArrayList<>();
    private final List<Integer> mMenu = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_menu;
    }

    @Override
    protected View getLayoutContainer() {
        return new View(mContext);
    }

    @Override
    protected void initView(View rootView) {
        mBanner.addBannerLifecycleObserver(this);
        CircleIndicator indicator = new CircleIndicator(getContext());
        mBanner.setIndicator(indicator);
        mAdapter = new MenuItemAdapter(getContext());
        recyclerView.setAdapter(mAdapter);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 6);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) return 6;
                if (position > 0 && position < 5) return 3;
                if (position > 5 && position < 12) return 2;
                return 6;
            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int itemCount = state.getItemCount();
                for (int i = 0; i < itemCount; i++) {
                    if (i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 10) {
                        outRect.left = 0;
                    }
                    outRect.right = SizeUtils.dp2px(4);
                    outRect.top = SizeUtils.dp2px(2);
                    outRect.bottom = SizeUtils.dp2px(2);
                }
            }
        });
        RecyclerView realRecyclerView = recyclerView.getRecyclerView();
        realRecyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
        //realRecyclerView.setHorizontalScrollBarEnabled(false);
        //realRecyclerView.setVerticalScrollBarEnabled(false);
    }

    @Override
    protected void lazyLoadData() {
        String urlJson = "{\"status\":200,\"message\":\"SUCCESS\",\"data\":[{\"slideshow\":\"http://www.cqvie.edu.cn/images/zhuanjia.jpg\"},{\"slideshow\":\"http://www.cqvie.edu.cn/images/rgznzjjtlbt.jpg\"},{\"slideshow\":\"http://www.cqvie.edu.cn/images/1234.jpg\"},{\"slideshow\":\"http://www.cqvie.edu.cn/images/12345.jpg\"},{\"slideshow\":\"http://www.cqvie.edu.cn/images/qt1.jpg\"}]}";
        SlideShow slideShow = GsonUtils.fromJson(urlJson, SlideShow.class);
        mDataBeans = slideShow.getData();
        mBanner.setAdapter(new ImageBannerAdapter(mDataBeans));
        for (int i = 0; i < 20; i++) {
            mMenu.add(i);
        }
        mAdapter.addAll(mMenu);
    }

    @Override
    protected void initEvent() {
        mBanner.setOnBannerListener((data, position) -> {
            // TODO: 2020/7/23 Banner点击之后的操作
        });
    }

    /**
     * 在界面重新可见的时候更改页面的主题
     */
    @Override
    protected void uiReShowing() {
        FragmentActivity activity = getActivity();
        if (activity instanceof HomeActivity) {
            HomeActivity homeActivity = (HomeActivity) activity;
            homeActivity.resetBgState();
        }
    }
}
